using System.Collections.Generic;
using UFrame.MapJson;
using UnityEditor;
using UnityEngine;


namespace UFrame
{
    public class SafeAreaEditor : MapEditorBase
    {

        /// <summary>
        /// 此类的一个全局对象
        /// </summary>
        private static SafeAreaEditor windows = null;

        /// <summary>
        /// 滚动位置
        /// </summary>
        public Vector2 rollPosition = Vector2.zero;

        /// <summary>
        /// 安全区数据
        /// </summary>
        List<SafeAreaInfo> data = new List<SafeAreaInfo>();

        /// <summary>
        /// 移出表数据索引
        /// </summary>
        List<int> index = new List<int>();

        List<int> delete = new List<int>();

        SceneView.OnSceneFunc m_delegate;

        /// <summary>
        /// 打开
        /// </summary>
        public static void OpenWindow()
        {
            if (windows == null)
            {
                windows = MapEditorBase.GetWindowWithRect<SafeAreaEditor>(new Rect(0, 0, 480, 320), false, "安全区域", true);
                windows.InitWindow();
                windows.Show();
            }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public SafeAreaEditor()
        {
            m_delegate = new SceneView.OnSceneFunc(OnSceneFunc);
            SceneView.onSceneGUIDelegate += m_delegate;
        }

        /// <summary>
        /// 初始化窗口
        /// </summary>
        private void InitWindow()
        {
            /*			XmlConfig.SafeAreaXmlConfig saxc = DataManagerInstance.instance.Get<XmlConfig.SafeAreaXmlConfig>();
                        if (saxc != null)
                        {
                            this.data = saxc.data;
                            for (int i = 0; i < data.Count; ++i)
                            {
                                for (int j = 0; j < data[i].safeArea.Count; ++j)
                                {
                                    data[i].safeArea[j].go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                                    data[i].safeArea[j].go.transform.localPosition = data[i].safeArea[j].position;
                                    data[i].safeArea[j].go.transform.localScale = Vector3.one * 10f;
                                }
                            }
                        }
                        index = new List<int>();
                        delete = new List<int>();*/
        }

        /// <summary>
        /// 窗口关闭时调用
        /// </summary>
        public void OnDestroy()
        {
            Destroy();
        }

        void Destroy()
        {
            if (data != null)
            {
                for (int i = 0; i < data.Count; ++i)
                {
                    for (int j = 0; j < data[i].safeArea.Count; ++j)
                    {
                        GameObject.DestroyImmediate(data[i].safeArea[j].go);
                    }
                }
            }
            index.Clear();
            delete.Clear();
            SceneView.onSceneGUIDelegate -= m_delegate;
        }

        /// <summary>
        /// 关闭
        /// </summary>
        public static void CloseWindow()
        {
            if (windows)
            {
                //销毁对象
                windows.Destroy();
                //关闭窗口
                windows.Close();
            }
        }

        /// <summary>
        /// GUI
        /// </summary>
        void OnGUI()
        {
            GUI.backgroundColor = Color.gray;
            GUILayout.BeginArea(new Rect(position.width * 0.01f, position.height * 0.02f, position.width * 0.98f, position.height * 0.74f));
            rollPosition = GUILayout.BeginScrollView(rollPosition);

            index.Clear();
            GUILayout.BeginVertical();
            for (int i = 0; i < data.Count; ++i)
            {
                GUILayout.BeginHorizontal();
                GUI.color = Color.yellow;
                GUILayout.Label("◇ SafeArea :");

                GUI.color = Color.green;
                if (GUILayout.Button("添加", GUILayout.Width(64)))
                {
                    SafeAreaPoint sap = new SafeAreaPoint();
                    sap.go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                    data[i].safeArea.Add(sap);
                    if (data[i].safeArea.Count >= 2)
                    {
                        sap.go.transform.position = data[i].safeArea[data[i].safeArea.Count - 2].go.transform.position;
                    }
                    else
                    {
                        sap.go.transform.position = SceneView.lastActiveSceneView.camera.transform.position + SceneView.lastActiveSceneView.camera.transform.forward * 10f;
                    }
                }

                GUI.color = Color.red;
                if (GUILayout.Button("删除", GUILayout.Width(64)))
                {
                    index.Add(i);
                }
                GUILayout.EndHorizontal();

                delete.Clear();
                for (int j = 0; j < data[i].safeArea.Count; ++j)
                {
                    GUILayout.BeginHorizontal();
                    GUI.color = Color.white;
                    GUILayout.Label("");
                    Object go = data[i].safeArea[j].go;
                    EditorGUILayout.ObjectField(go, typeof(GameObject));

                    GUI.color = Color.green;
                    if (GUILayout.Button("选中", GUILayout.Width(64)))
                    {
                        Selection.activeGameObject = data[i].safeArea[j].go;
                    }
                    GUI.color = Color.red;
                    if (GUILayout.Button("delete", GUILayout.Width(64)))
                    {
                        delete.Add(j);
                    }
                    GUILayout.EndHorizontal();
                }
                for (int j = 0; j < delete.Count; ++j)
                {
                    GameObject.DestroyImmediate(data[i].safeArea[delete[j]].go);
                    data[i].safeArea.RemoveAt(delete[j]);
                }
            }

            GUILayout.EndVertical();
            GUILayout.EndScrollView();
            GUILayout.EndArea();

            for (int i = 0; i < index.Count; ++i)
            {

                for (int j = 0; j < data[index[i]].safeArea.Count; ++j)
                {
                    GameObject.DestroyImmediate(data[index[i]].safeArea[j].go);
                }

                data.RemoveAt(index[i]);
            }

            GUI.color = Color.green;

            if (GUI.Button(new Rect(position.width * 0.01f, position.height * 0.78f, position.width * 0.98f, position.height * 0.07f), "+"))
            {
                SafeAreaInfo sai = new SafeAreaInfo();
                data.Add(sai);
            }

            if (GUI.Button(new Rect(position.width * 0.01f, position.height * 0.86f, position.width * 0.98f, position.height * 0.12f), "保存"))
            {
                Save();
            }
            GUI.color = Color.gray;


        }



        /// <summary>
        /// 保存数据
        /// </summary>
        public override void Save()
        {
            for (int i = 0; i < data.Count; ++i)
            {
                for (int j = 0; j < data[i].safeArea.Count; ++j)
                {
                    data[i].safeArea[j].position = data[i].safeArea[j].go.transform.localPosition;
                }
            }

            isChange = false;
        }

        void OnSceneFunc(SceneView sceneView)
        {
            try
            {
                Handles.color = Color.red;
                Handles.lighting = true;
                for (int i = 0; i < data.Count; ++i)
                {
                    List<Vector3> array = new List<Vector3>();
                    for (int j = 0; j < data[i].safeArea.Count; ++j)
                    {
                        array.Add(data[i].safeArea[j].go.transform.localPosition);
                    }
                    if (array.Count >= 2)
                    {
                        array.Add(array[0]);
                        Handles.DrawPolyLine(array.ToArray());
                    }
                }
            }
            catch
            {
                Destroy();
            }
        }
    }
}

